package com.bw.unit6.arouter

import android.content.Context
import android.text.TextUtils
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Interceptor
import com.alibaba.android.arouter.facade.callback.InterceptorCallback
import com.alibaba.android.arouter.facade.template.IInterceptor
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils

/**
 *
 * @ClassName: LoginInterceport
 * @Description: java类作用描述
 * @Author: zhangyanchao
 * @CreateDate: 2024/9/24 11:57 AM
 * @Version: 1.0
 */
@Interceptor(priority = 1, name = "login_interceptor")
class LoginInterceptor : IInterceptor {
    override fun init(context: Context?) {
    }

    override fun process(postcard: Postcard?, callback: InterceptorCallback?) {
        //如果没有登录，跳转到登录页面-LoginActivity
        //如果已经登录 继续当前路由跳转
        //postcard?.path 目标页面路径
        // postcard?.path =  "/main/MainActivity"
        if (TextUtils.isEmpty(SPUtils.getInstance().getString("token"))
            && !postcard?.path.equals("/login/123")) {
            //跳转到登录页面
            ARouter.getInstance().build("/login/123").navigation()

            callback?.onInterrupt(null)
        }else{
            //如果已经登录，或者当前跳转路由路径是登录页面：不拦截，继续路由跳转
            callback?.onContinue(postcard)
        }
    }
}